﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace MyBatis_tools
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            this.Reset();
        }

        public void Reset()
        {

            if (!File.Exists(@"info.json"))
            {
                return;
            }


            string config = File.ReadAllText(@"info.json");
            JObject obj = JObject.Parse(config);
            this.txtBasePath.Text = obj["basePath"].ToString();
            this.txtBasePackage.Text = obj["basePackage"].ToString();
            this.txtModular.Text = obj["modular"].ToString();
            this.txtPrefix.Text = obj["prefix"].ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("basePath", this.txtBasePath.Text);
            dic.Add("basePackage", this.txtBasePackage.Text);
            dic.Add("modular", this.txtModular.Text);
            dic.Add("prefix", this.txtPrefix.Text);
            dic.Add("tablesNames", this.richTableNames.Text.Split('\n'));
            string output = JsonConvert.SerializeObject(dic);
            File.WriteAllText(@"info.json", output);
            RunCmd("java -jar -Dfile.encoding=UTF-8 mybatis-tools-1.0-SNAPSHOT.jar");
            MessageBox.Show("生成完成");


        }
        public string RunCmd(string cmd)
        {
            Process proc = new Process();
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.Start();
            proc.StandardInput.WriteLine(cmd);
            proc.StandardInput.WriteLine("exit");
            string outStr = proc.StandardOutput.ReadToEnd();
            proc.Close();
            return outStr;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Reset();
        }
    }
}
